<template>

  <div style="display: flex; flex-direction: column; align-items: center;">
    <el-table :data="tableData" >
      <el-table-column prop="textbookId" label="教材Id">
      </el-table-column>
      <el-table-column prop="stock" label="库存">
      </el-table-column>
      <el-table-column prop="courseName" label="课程名">
      </el-table-column>
      <el-table-column prop="departmentId" label="系Id">
      </el-table-column>
      <el-table-column prop="pressId" label="出版社Id">
      </el-table-column>
      <el-table-column prop="price" label="标价">
      </el-table-column>
      <el-table-column prop="textbookName" label="书名">
      </el-table-column>
    </el-table>



    <el-pagination
        :page-size="pageSize"
        :pager-count="pageNum"
        :total="totalCount"
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        >
    </el-pagination>

    <el-form v-if="userRole === 'department'||userRole === 'office'" :inline="true">
      <el-form-item>
        <el-input v-model="damageTextbookId" @input="handleNumberInput('changeTextbookId')" placeholder="教材Id"></el-input>
      </el-form-item>
      <!--
      <el-form-item>
        <el-input v-model="damageTextbookName" placeholder="教材名"></el-input>
      </el-form-item>
      -->
      <el-form-item>
        <el-input v-model="damageNum" @input="handleNumberInput('changePublisherId')" placeholder="报损数目"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="ReportDamageText" style="background-color: green; border-color: green;">报损</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import request from "@/utils/request";
import {Message} from "element-ui";
import userManager from "@/UserManager";

export default {
  data() {
    return {
      pageSize: 13,
      pageNum: 1,
      totalCount: 0,
      tableData: [],
      damageTextbookId:'',
      damageTextbookName:'',
      damageNum:'',
      userRole:userManager.getPermissions()
    }
  },

  mounted() {
    this.GetAllTextbook();
  },
  methods: {

    GetAllTextbook() {

      const GetAllTextbookRequest = {

        pageNum:this.pageNum,
        pageSize:this.pageSize,

      }
      request.post("/textbook/queryAllTextbook", GetAllTextbookRequest)
          .then(res => {
            console.log(res.data);

            this.tableData = res.data.data.list;
            this.totalCount = res.data.data.totalCount;

          })
          .catch(error => {
            console.error("Error fetching data:", error);
          });
    },
    ReportDamageText() {

      const ReportDamageTextRequest = {
        textbookId:Number(this.damageTextbookId),
        //damageTextbookName:this.damageTextbookName,
        loseNum:Number(this.damageNum)

      }
      request.post("/textbook/loseReq", ReportDamageTextRequest)
          .then(res => {

            this.GetAllTextbook();

          })
          .catch(error => {
            console.error("Error fetching data:", error);
          });
    },

    showError(str) {
      // 触发错误提示
      Message.error({
        message: str,
        duration: 3000 // 持续时间为3000毫秒（3秒）
      });
    },
    showMsg(str) {
      // 触发错误提示
      Message.log({
        message: str,
        duration: 3000 // 持续时间为3000毫秒（3秒）
      });
    },
    handleNumberInput(field) {
      this.form[field] = this.form[field].replace(/\D/g, ''); // 只保留数字字符
    },

    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.GetAllTextbook();
    },

  }
};
</script>
